package com.itheima.CSM_bacK.aabServices;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.CSM_bacK.aabServices.Serv.CheckSetmealServ;
import com.itheima.CSM_bacK.aacMapper.CheckSetmealMapper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.pojo.DTO.DTOcSetmealA;
import com.itheima.pojo.DTO.DTOcSetmealE;
import com.itheima.pojo.VO.VOcSetmealFBI;
import com.itheima.pojo.Entity.Setmeal;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckSetmealImpl implements CheckSetmealServ {
    @Autowired
    private CheckSetmealMapper checkSetmealMapper;


    @Override
    public void add(List<Integer> checkgroupIds, DTOcSetmealA kk) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(kk, setmeal);
        checkSetmealMapper.add(setmeal);
        int setmealId = setmeal.getId();
        for (int i = 0; i < checkgroupIds.size(); i++) {
            checkSetmealMapper.addAtConnection(checkgroupIds.get(i),setmealId);
        }

    }

    @Override
    public void edit(List<Integer> checkGroupIds, DTOcSetmealE kk) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(kk, setmeal);
        checkSetmealMapper.edit(setmeal);
        int setmealId = setmeal.getId();
        checkSetmealMapper.deleteAtConnection(setmealId);
        for (int i = 0; i < checkGroupIds.size(); i++) {
            checkSetmealMapper.addAtConnection(checkGroupIds.get(i),setmealId);
        }
    }

    @Override
    public VOcSetmealFBI findById(Integer id) {
        Setmeal kk = checkSetmealMapper.findById(id);
        VOcSetmealFBI vv = new VOcSetmealFBI();
        BeanUtils.copyProperties(kk, vv);
        return vv;
    }

    @Override
    public PageResult findPage(QueryPageBean kk) {
        PageHelper.startPage(kk.getCurrentPage(), kk.getPageSize());
        Page<Setmeal> pps = checkSetmealMapper.findPage(kk.getQueryString());
        return new PageResult(pps.getTotal(), pps.getResult());
    }
}
